package io.github.harmonly.service.impl;

import io.github.harmonly.entity.User;
import io.github.harmonly.mapper.UserMapper;
import io.github.harmonly.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper mapper;

    public boolean auth(HttpSession session, String account, String password) {
        User user = mapper.getUserByAccount(account);
        if (user == null) return false;
        boolean flag = user.getPassword().equals(password);
        if (flag) session.setAttribute("user", user);
        return flag;
    }

    @Override
    public boolean addUser(User user) {
        return mapper.addUser(user) == 1;
    }

    @Override
    public User getUser(int uid) {
        return mapper.getUser(uid);
    }

    @Override
    public List<User> getUsers() {
        return mapper.getUsers();
    }

    @Override
    public List<User> getUsersByKeyWord(String keyword) {
        return mapper.getUsersByKeyWord(keyword);
    }

    @Override
    public boolean deleteUser(int uid) {
        return mapper.deleteUser(uid) == 1;
    }

    @Override
    public boolean editUser(User user) {
        return mapper.editUser(user) == 1;
    }
}
